﻿#region Imports
using System;
using System.Globalization;
using System.Windows.Data;
using CommonUtilities.Extensions;
using CommonUtilities.Properties;
#endregion

namespace CommonUtilities.Converters
{
    /// <summary>
    /// Converts an invalid date to user-friendly text
    /// </summary>
    public class InvalidDateConverter : IValueConverter
    {
        #region IValueConverter Members
        /// <summary>
        /// Converts an invalid <code>System.DateTime</code> to user-friendly text
        /// </summary>
        /// <param name="value">A <code>System.DateTime</code> value</param>
        /// <param name="targetType"><code>System.String</code></param>
        /// <param name="parameter">Date format</param>
        /// <param name="culture">UI culture of thread</param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime dt = DateTime.MinValue;
            string format = "dd MMM yyyy";

            try { dt = (DateTime)value; }
            catch (Exception) { }

            if (!(parameter as string).IsNullOrEmpty()) format = parameter as string;
            return dt == DateTime.MinValue ? Resources.Const_NotAvailable : dt.ToString(format);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion IValueConverter Members
    }
}
